package com.nibian.nibian.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nibian.nibian.R;


/**
 * @创建者 lucy
 * @创建时间 2016/10/13 15:04
 * @描述 ${进度圈}
 * @更新者 $Author$
 * @更新时间 $Date$
 * @更新描述 ${TODO}
 */
public class ProgressDialogs {

    @SuppressLint("InflateParams")
    public static Dialog getLoading(Context context, String message, final boolean isNeedDelay) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);

        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);

        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);

        TextView text = (TextView) v.findViewById(R.id.text);
        if (message != null && message.length() > 0)
            text.setText(message);

        final Dialog dialog = new Dialog(context, R.style.LoadingDialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (dialog != null && isNeedDelay) {
                    dialog.dismiss();
                }
            }
        }, 1500);

        return dialog;
    }

    public static Dialog getLoading(Context context, String message) {
        return getLoading(context, message, true);
    }
}
